package com.google.blockly.ohos.demo.view;

import com.google.blockly.ohos.ResourceTable;
import com.google.blockly.ohos.demo.DevTestsAbility;
import com.google.blockly.ohos.demo.EventHelper;
import ohos.agp.components.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class DevTestsAbilityTest {
    private Component menu;
    private DevTestsAbility ability;

    @Before
    public void setUp() throws Exception {
        ability = EventHelper.startAbility(DevTestsAbility.class);
        menu = LayoutScatter.getInstance(EventHelper.sAbilityDelegator.getAppContext()).parse(getDefaultMenuLayoutRes(), null, false);
    }

    @After
    public void tearDown() throws Exception {
        EventHelper.clearAbilities();
    }

    @Test
    public void clickSectionS() throws Exception {
        DevTestsAbility ability = EventHelper.startAbility(DevTestsAbility.class);
        Image image = (Image) ability.findComponentById(ResourceTable.Id_action_sections);
        EventHelper.triggerClickEvent(ability, image);
        Thread.sleep(5000);
        Assert.assertNotNull("It's right", image);
    }

    @Test
    public void clickSectionS0() throws Exception {
        DevTestsAbility ability = EventHelper.startAbility(DevTestsAbility.class);
        Image image = (Image) ability.findComponentById(ResourceTable.Id_action_sections);
        EventHelper.triggerClickEvent(ability, image);
        Thread.sleep(1000);
        ListContainer mSectionsListView = (ListContainer) LayoutScatter.getInstance(EventHelper.sAbilityDelegator.getAppContext()).parse(ResourceTable.Layout_sections_list, null, false);
        EventHelper.triggerClickEvent(ability, mSectionsListView.getComponentAt(0));
        Thread.sleep(2000);
        Assert.assertNotNull("It's right", image);
    }

    @Test
    public void clickSectionS1() throws Exception {
        DevTestsAbility ability = EventHelper.startAbility(DevTestsAbility.class);
        Image image = (Image) ability.findComponentById(ResourceTable.Id_action_sections);
        EventHelper.triggerClickEvent(ability, image);
        Thread.sleep(1000);
        ListContainer mSectionsListView = (ListContainer) LayoutScatter.getInstance(EventHelper.sAbilityDelegator.getAppContext()).parse(ResourceTable.Layout_sections_list, null, false);
        EventHelper.triggerClickEvent(ability, mSectionsListView.getComponentAt(1));
        Thread.sleep(2000);
        Assert.assertNotNull("It's right", image);
    }

    @Test
    public void clickSectionS2() throws Exception {
        DevTestsAbility ability = EventHelper.startAbility(DevTestsAbility.class);
        Image image = (Image) ability.findComponentById(ResourceTable.Id_action_sections);
        EventHelper.triggerClickEvent(ability, image);
        Thread.sleep(1000);
        ListContainer mSectionsListView = (ListContainer) LayoutScatter.getInstance(EventHelper.sAbilityDelegator.getAppContext()).parse(ResourceTable.Layout_sections_list, null, false);
        EventHelper.triggerClickEvent(ability, mSectionsListView.getComponentAt(2));
        Thread.sleep(2000);
        Assert.assertNotNull("It's right", image);
    }

    @Test
    public void clickRun() throws Exception {
        Text text = (Text) menu.findComponentById(ResourceTable.Id_action_run);
        EventHelper.triggerClickEvent(ability, text);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", text);
    }

    @Test
    public void clickClear() throws Exception {
        Text text = (Text) menu.findComponentById(ResourceTable.Id_action_clear);
        EventHelper.triggerClickEvent(ability, text);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", text);
    }

    @Test
    public void clickAirstrike() throws Exception {
        Text text = (Text) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_action_airstrike);
        EventHelper.triggerClickEvent(ability, text);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", text);
    }

    @Test
    public void clickCarpet_bomb() throws Exception {
        Text text = (Text) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_action_carpet_bomb);
        EventHelper.triggerClickEvent(ability, text);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", text);
    }

    @Test
    public void clickCreate_variable() throws Exception {
        Text text = (Text) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_action_create_variable);
        EventHelper.triggerClickEvent(ability, text);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", text);
    }

    @Test
    public void clickScrollableMenuItem() throws Exception {
        Checkbox mScrollableMenuItem = (Checkbox) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_scrollable_menuitem);
        EventHelper.triggerClickEvent(ability, mScrollableMenuItem);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", mScrollableMenuItem);
    }

    @Test
    public void clickPinchZoomMenuItem() throws Exception {
        Checkbox mPinchZoomMenuItem = (Checkbox) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_pinch_zoom_menuitem);
        EventHelper.triggerClickEvent(ability, mPinchZoomMenuItem);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", mPinchZoomMenuItem);
    }

    @Test
    public void clickLogEventsMenuItem() throws Exception {
        Checkbox mLogEventsMenuItem = (Checkbox) menu.findComponentById(com.google.blockly.ohos.demo.ResourceTable.Id_log_events_menuitem);
        EventHelper.triggerClickEvent(ability, mLogEventsMenuItem);
        Thread.sleep(1000);
        Assert.assertNotNull("It's right", mLogEventsMenuItem);
    }

    public int getDefaultMenuLayoutRes() {
        return com.google.blockly.ohos.demo.ResourceTable.Layout_dev_tests_menu;
    }
}